package jyj.springmvc.learning.controller;

/**
 * @Author 老顽固小可爱
 * @Date 2022/12/12 13:43
 * @Version 1.0
 */

import jyj.springmvc.learning.bean.User;
import jyj.springmvc.learning.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author C语言中文网
 */
@Controller
public class LoginController {
    @Autowired
    private UserDao userDao;

    /**
     * 登录
     *
     * @param user
     * @param request
     * @return
     */
    @RequestMapping("/login")
    public String login(User user, HttpServletRequest request) {
        User loginUser = userDao.getUserByUserName(user.getUserName());
        if (loginUser != null && loginUser.getPassword().equals(user.getPassword())) {
            HttpSession session = request.getSession();
            //将用户信息放到 session 域中
            session.setAttribute("loginUser", loginUser);
            //重定向到商品列表
            return "redirect:/products";
        }
        request.setAttribute("msg", "账号或密码错误");
        return "login";
    }
}
